<?php
if(!defined('IN_ADMIN')) exit('Access Denied');

class games extends app
{


    function main()
    {
        global $_G;
        $size = 30;
        $page = intval($_GET['page']);
        if ($page < 1) $page = 1; // 第一页

        $base_url = URL . 'm=' . CURMODULE;
        $this->add(array('base_url' => $base_url));
        $post_url = $base_url . '&a=' . CURACTION;
        $page_url = $post_url;
        $total_count = DB::result_first("SELECT COUNT(`id`) AS `count` FROM " . DB::table('games'));
        $base_url = URL . 'm=' . CURMODULE;
        if (empty($total_count)) $total_count = 0;
        if (0 == $total_count % $size) {
            $total_page = $total_count / $size;
        } else {
            $total_page = ($total_count - $total_count % $size) / $size + 1;
        }

        if ($page > $total_page && $total_page > 0) $page = $total_page;

        $page_nums = array();
        for ($i = 0; $i < $total_page; $i++) {
            $page_nums[] = $i + 1;
        }
        $this->add(array('total_count' => $total_count, 'page' => $page, 'total_page' => $total_page, 'page_nums' => $page_nums, 'page_url' => $page_url, 'base_url' => $base_url));
        $page2 = $_GET['page'];
        if ($page2 == 1 || empty($page2) || $page2 == '请选择页码') {
            $start = 0;
        } else {
            $start = ($page2 - 1) * $size;
        }
        $game_info=DB::fetch_all("select * from ".DB::table('games')." order by createtime desc");
        $this->add(array('game_info'=>$game_info));
        $this->show();
    }


    function add_game()
    {
        $post = $_POST;
      $post['createtime']=TIMESTAMP;
      $post['updatetime']=TIMESTAMP;
       if(!empty($post) && empty($_FILES['file'])){
           $result = DB::insert('games', $post, true);
           session_start();
           $_SESSION['result']=$result;

       }

        if (!empty($_FILES['file'])) {
          $gameimg='https://testweapp.whwangdoudou.cn/classic_games'.$this->upload();
          DB::update('games',array('gameimg'=>$gameimg),array('id'=>$_SESSION['result']));

        }

        if ($result > 0) {
            echo json_encode(array('r' => 1));
        }
        echo json_encode(array('r' => -1));
        exit;
    }

    function upload($file, $type)
    {
        global $_G;
        if (!$file && !$_FILES[file]) return false;
        if (is_string($file) && $file) {
            $file = str_replace(ROOT_PATH, '', $file);
            $target_file = ROOT_PATH . $file;
            if (!file_exists($target_file)) return false;
        } elseif ($file['tmp_name']) {
            $target_file = $file['tmp_name'];
        } else if ($_FILES['file'] && $_FILES['file']['tmp_name']) {
            $target_file = $_FILES['file']['tmp_name'];

            $file = $_FILES['file'];
        } else {
            return false;
        }


        if (!is_string($target_file)) return;
        $class = 'web';

        if (!class_exists($class . '_upload')) include_once ROOT_PATH . 'inc/class/upload/' . $class . '_upload.php';
        $cname = $class . '_upload';

        $bc = new $cname();
        $bc->file = &$file;

        if (!is_file($target_file)) return false;
        $rs = $bc->upload($target_file);

        if ($rs && is_string($rs)) {
            @unlink($target_file);
            return $rs;
        } else {
            $msg = ('图片上传错误,错误代码:' . $bc->code . ',错误信息:' . $bc->msg);
            L($msg);
        }
    }

    function find(){
        $post=$_POST;
        if(!empty($post) && isset($post)){
           $game_info=DB::fetch_first("select * from ".DB::table('games')." where id=".$post['id']);
            if(!empty($game_info) && isset($game_info)){
                echo json_encode(array('result'=>$game_info,'r'=>0));exit;
            }else{
                echo json_encode(array('r'=>-1));
            }
        }
    }


    function  editor(){
        $post = $_POST;
        $post['updatetime']=TIMESTAMP;
        if(!empty($post) && empty($_FILES['file'])){
            $result = DB::update('games', $post, array('id'=>$post['id']));
            session_start();
            $_SESSION['editor']=$post['id'];
            }

        if (!empty($_FILES['file'])) {
            $gameimg='https://testweapp.whwangdoudou.cn/classic_games'.$this->upload();
            DB::update('games',array('gameimg'=>$gameimg),array('id'=>$_SESSION['editor']));

        }

        if ($result > 0) {
            echo json_encode(array('r' => 1));
        }
        echo json_encode(array('r' => -1));
        exit;
    }


    function setting() {
        global $_G;

        $check_we_app = DB::result_first("SELECT `value` FROM " . DB::table('setting') . " WHERE `name`='game_v1'");
        $result = 0;
        if (!empty($check_we_app) && 1 == intval($check_we_app)) {
            $result = 1;
        }

        $this->add(array('check_we_app' => $result));
        $this->show();
    }


    function setting_save() {
        $post = $_POST;

        $check_we_app = $post['check_we_app'];
        DB::update('setting', array('value' => $check_we_app), "name='game_v1'");
        echo json_encode(array('r' => 0));
    }


}
?>